import { Component, Input, OnInit } from '@angular/core';
import { SFSchema } from '@delon/form';
import { ApiService } from '@core/services/api';
import { HttpClient } from '@angular/common/http';
import { NzMessageService, NzModalRef } from 'ng-zorro-antd';
import { THIS_EXPR } from '@angular/compiler/src/output/output_ast';

@Component({
  selector: 'person-edit',
  templateUrl: './person-edit.component.html',
  styles: []
})
export class PersonEditComponent implements OnInit {
  @Input()
  set _param(value) {
    if (!value) return;
    this.param = value;
  };
  param: any;
  showForm = false;
  schema: SFSchema = {
    properties: {
      dept: {
        type: 'string',
        title: '责任人',
        enum: [],
        ui: {
          widget: 'select',
          mode: 'multiple',
          dropdownStyle: {
            'max-height': '300px'
          }
        }
      }
    },
    required: []
  }
  getPersonList(): Promise<any>{
    const res = this.http.request<any>('POST', '/core/host/person/'+this.param.id).toPromise();
    return res;
  }
  formSubmit(data) {
    const body: any = {
      userName: data.userName,
      userAccount: data.userAccount,
      status: data.status,
      userRemark: data.userRemark,
      deviceId: data.id,
    };
    body.depts = data.dept.map(item => {
      return {id: item}
    });
    const url = '/core/host/person';
    const method = 'POST';
    this.http.request<any>(method, url,  { body}).subscribe(res => {
      console.log(res)
      if (res.status === '200' || res.status === '201') {
        this.msg.success('操作成功');
        this.modalRef.destroy('ok');
      } else {
        this.msg.error(res.message)
      }
    })

  }
  constructor(
    private api: ApiService,
    private http: HttpClient,
    private msg: NzMessageService,
    private modalRef: NzModalRef
  ) { }

  async ngOnInit() {
    await this.getPersonList().then(res => {
      this.schema.properties.dept.enum = res.data.map(item => {
        return {label: item.roleName, value: item.id}
      })
    })
    this.showForm = true;
  }

}
